package com.xqls.biz.impl;

import com.xqls.bean.RainwaterDischargeRealdataBean;
import com.xqls.biz.AdministrationBiz;
import com.xqls.biz.RainwaterDischargeRealdataBiz;
import com.xqls.dal.model.RainwaterDischargeRealdata;
import com.xqls.redis.Constants;
import com.xqls.redis.RedisCache;
import com.xqls.redis.RedisKeyEnum;
import com.xqls.service.RainwaterDischargeRealdataService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RainwaterDischargeRealdataBizImpl implements RainwaterDischargeRealdataBiz{
	private static Logger logger = Logger.getLogger(RainwaterDischargeRealdataBiz.class);
	
	@Autowired RainwaterDischargeRealdataService realdataService;
	@Autowired private AdministrationBiz administrationBiz;
	@Autowired
	private RedisCache<String, Object> redisCache;
	@Override
	public CommonResult queryRainwaterDischargeRealdata(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
		CommonResult commonResult=new CommonResult();
		try {
			List<String> columns_name = new ArrayList<String>();
			List<Object> columns = new ArrayList<Object>();
			List<String> displayfiled = administrationBiz.conversion("rainwater_discharge_realdata");
			columns.add("viewId");
			for (String display : displayfiled) {
				if(!"device_no".equals(display)){
					columns.add(display);

				}
			}
			List<String> fieldalias = administrationBiz.fieldalias("rainwater_discharge_realdata");

			columns_name.add("序号");
			for (String fieldalia : fieldalias) {
				if (!"编号".equals(fieldalia)) {
					columns_name.add(fieldalia);
				}
			}
//			commonPage.setPageData(realdataService.selectByMap(commonPage.pageToMap()));
			List<Map<String, Object>> datas = realdataService.selectByMap(commonPage.pageToMap());
			int curr=commonPage.getCurr();
			for (int i = 0;i<datas.size();i++){
				datas.get(i).put("viewId", i + 1+((curr-1)*15));
			}
			commonPage.setPageData(datas);
			Map<String,Object> showMap=new HashMap<String,Object>();
			showMap.put("realdataList",commonPage.getPageData());
			showMap.put("userPerms", redisCache.get(Constants.getRedisKey(RedisKeyEnum.USER_PERMS, SecurityUtils.getSubject().getPrincipal().toString())));

			showMap.put("columns",  CommonPage.setColumnToPage(columns_name.toArray(new String[columns_name.size()]),
					columns.toArray(), null, "企业雨水排放口实时数据"));
			commonPage.setHtml(VelocityUtil.getHtmlByMap("Realdata/realdataListData.vm",showMap,request,response,request.getSession().getServletContext()));
			commonResult.setIsSuccess(true);
		} catch (Exception e) {
			logger.error("企业雨水排放口实时数据查询异常",e);
		}
		return commonResult;
	}
	


	public List<Map<String, Object>> queryAll(){
		List<Map<String, Object>> list=null;
		try {
			list=realdataService.queryAll();
		} catch (Exception e) {
			logger.error("查询所有企业雨水排放口数据异常", e);
		}
		return list;
	}
	
	
	public RainwaterDischargeRealdataBean queryByDeviceNo(String deviceNo){
        RainwaterDischargeRealdataBean realdata=null;
		try {
			 realdata=	realdataService.queryByDeviceNo(deviceNo);

		} catch (Exception e) {
			logger.error("根据设备编号查询企业雨水排放口数据异常", e);
		}
		return realdata;

	}
	
	public RainwaterDischargeRealdata selectByDeviceNo(String deviceNo){
		return realdataService.selectByDeviceNo(deviceNo);
	}

}
